package com.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.tools.CommonResult;
import com.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;

import com.entity.ShangpinxinxiEntity;
import com.entity.view.ShangpinxinxiView;

import com.service.ShangpinxinxiService;
import com.service.TokenService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MD5Util;
import com.utils.MPUtil;
import com.utils.CommonUtil;

import static cn.hutool.core.date.DateTime.now;


/**
 * 商品信息
 * 后端接口
 * @author
 * @email
 * @date 2021-04-21 20:42:06
 */
@RestController
@RequestMapping("/shangpinxinxi")
@Api(tags = "商品信息模块")
@CrossOrigin
public class ShangpinxinxiController {
	@Autowired
	private ShangpinxinxiService shangpinxinxiService;

	/**
	 * 后端列表
	 */
	@ApiOperation("后端列表")
	@GetMapping("/page")
	public R page(@RequestParam Map<String, Object> params,ShangpinxinxiEntity shangpinxinxi,
				  HttpServletRequest request){
		EntityWrapper<ShangpinxinxiEntity> ew = new EntityWrapper<ShangpinxinxiEntity>();
		PageUtils page = shangpinxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, shangpinxinxi), params), params));
		return R.ok().put("data", page);
	}

	/**
	 * 前端列表
	 */
	@ApiOperation("前端列表")
	@IgnoreAuth
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params,ShangpinxinxiEntity shangpinxinxi,
				  HttpServletRequest request){
		EntityWrapper<ShangpinxinxiEntity> ew = new EntityWrapper<ShangpinxinxiEntity>();
		PageUtils page = shangpinxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, shangpinxinxi), params), params));
		return R.ok().put("data", page);
	}

	/**
	 * 列表
	 */
	@ApiOperation("列表")
	@GetMapping("/lists")
	public R list( ShangpinxinxiEntity shangpinxinxi){
		EntityWrapper<ShangpinxinxiEntity> ew = new EntityWrapper<ShangpinxinxiEntity>();
		ew.allEq(MPUtil.allEQMapPre( shangpinxinxi, "shangpinxinxi"));
		return R.ok().put("data", shangpinxinxiService.selectListView(ew));
	}

	/**
	 * 查询
	 */
	@ApiOperation("查询")
	@GetMapping("/query")
	public R query(ShangpinxinxiEntity shangpinxinxi){
		EntityWrapper< ShangpinxinxiEntity> ew = new EntityWrapper< ShangpinxinxiEntity>();
		ew.allEq(MPUtil.allEQMapPre( shangpinxinxi, "shangpinxinxi"));
		ShangpinxinxiView shangpinxinxiView =  shangpinxinxiService.selectView(ew);
		return R.ok("查询商品信息成功").put("data", shangpinxinxiView);
	}

	/**
	 * 后端详情
	 */
	@ApiOperation("后端详情")
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") Long id){
		ShangpinxinxiEntity shangpinxinxi = shangpinxinxiService.selectById(id);
		shangpinxinxi.setClicknum(shangpinxinxi.getClicknum()+1);
		shangpinxinxi.setClicktime(new Date());
		shangpinxinxiService.updateById(shangpinxinxi);
		return R.ok().put("data", shangpinxinxi);
	}

	/**
	 * 前端详情
	 */
	@ApiOperation("前端详情")
	@IgnoreAuth
	@GetMapping("/detail/{id}")
	public R detail(@PathVariable("id") Long id){
		ShangpinxinxiEntity shangpinxinxi = shangpinxinxiService.selectById(id);
		shangpinxinxi.setClicknum(shangpinxinxi.getClicknum()+1);
		shangpinxinxi.setClicktime(new Date());
		shangpinxinxiService.updateById(shangpinxinxi);
		return R.ok().put("data", shangpinxinxi);
	}




	/**
	 * 后端保存
	 */
	@ApiOperation("后端保存")
	@PostMapping("/save")
	public R save(@RequestBody ShangpinxinxiEntity shangpinxinxi, HttpServletRequest request){
		System.out.println(shangpinxinxi);
		shangpinxinxi.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
		shangpinxinxi.setAddtime(now());
		//ValidatorUtils.validateEntity(shangpinxinxi);
		shangpinxinxiService.insert(shangpinxinxi);
		return R.ok();
	}

	/**
	 * 前端保存
	 */
	@ApiOperation("前端保存")
	@PostMapping("/add")
	public R add(@RequestBody ShangpinxinxiEntity shangpinxinxi, HttpServletRequest request){
		shangpinxinxi.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
		//ValidatorUtils.validateEntity(shangpinxinxi);
		shangpinxinxiService.insert(shangpinxinxi);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@ApiOperation("修改")
	@PostMapping("/update")
	public R update(@RequestBody ShangpinxinxiEntity shangpinxinxi, HttpServletRequest request){
		//ValidatorUtils.validateEntity(shangpinxinxi);
		shangpinxinxiService.updateById(shangpinxinxi);//全部更新
		return R.ok();
	}


	/**
	 * 删除
	 */
	@ApiOperation("删除")
	@PostMapping("/delete")
	public R delete(long id){
		shangpinxinxiService.deleteById(id);
		return R.ok();
	}

	/**
	 * 提醒接口
	 */
	@ApiOperation("提醒接口")
	@PostMapping("/remind/{columnName}/{type}")
	public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request,
						 @PathVariable("type") String type,@RequestParam Map<String, Object> map) {
		map.put("column", columnName);
		map.put("type", type);

		if(type.equals("2")) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			Date remindStartDate = null;
			Date remindEndDate = null;
			if(map.get("remindstart")!=null) {
				Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
				c.setTime(new Date());
				c.add(Calendar.DAY_OF_MONTH,remindStart);
				remindStartDate = c.getTime();
				map.put("remindstart", sdf.format(remindStartDate));
			}
			if(map.get("remindend")!=null) {
				Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
				c.setTime(new Date());
				c.add(Calendar.DAY_OF_MONTH,remindEnd);
				remindEndDate = c.getTime();
				map.put("remindend", sdf.format(remindEndDate));
			}
		}

		Wrapper<ShangpinxinxiEntity> wrapper = new EntityWrapper<ShangpinxinxiEntity>();
		if(map.get("remindstart")!=null) {
			wrapper.ge(columnName, map.get("remindstart"));
		}
		if(map.get("remindend")!=null) {
			wrapper.le(columnName, map.get("remindend"));
		}


		int count = shangpinxinxiService.selectCount(wrapper);
		return R.ok().put("count", count);
	}

	/**
	 * 前端智能排序
	 */
	@ApiOperation("前端智能排序")
	@IgnoreAuth
	@GetMapping("/autoSort")
	public R autoSort(@RequestParam Map<String, Object> params,ShangpinxinxiEntity shangpinxinxi, HttpServletRequest request,String pre,String num){
		EntityWrapper<ShangpinxinxiEntity> ew = new EntityWrapper<ShangpinxinxiEntity>();
		Map<String, Object> newMap = new HashMap<String, Object>();
		Map<String, Object> param = new HashMap<String, Object>();
		Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Object> entry = it.next();
			String key = entry.getKey();
			String newKey = entry.getKey();
			if (pre.endsWith(".")) {
				newMap.put(pre + newKey, entry.getValue());
			} else if (StringUtils.isEmpty(pre)) {
				newMap.put(newKey, entry.getValue());
			} else {
				newMap.put(pre + "." + newKey, entry.getValue());
			}
		}
		if(num.equals("1")){
			params.put("sort", "clicknum");
		}else if(num.equals("0")){
			params.put("sort", "price");

		}else if(num.equals("2")){
			params.put("sort","yuexiaoliang");
		}
		System.out.println("1"+params);
		params.put("order", "desc");
		PageUtils page = shangpinxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, shangpinxinxi), params), params));
		return R.ok().put("data", page);
	}


}
